#include "sprite.h"
#include "func.h"
	
/*PFNGLCREATEPROGRAMPROC glCreateProgram;
PFNGLDELETEPROGRAMPROC glDeleteProgram;
PFNGLCREATESHADERPROC glCreateShader;
PFNGLDELETESHADERPROC glDeleteShader;
PFNGLSHADERSOURCEPROC glShaderSource;
PFNGLCOMPILESHADERPROC glCompileShader;
PFNGLATTACHSHADERPROC glAttachShader;
PFNGLDETACHSHADERPROC glDetachShader;
PFNGLLINKPROGRAMPROC glLinkProgram;
PFNGLUSEPROGRAMPROC glUseProgram;
PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
PFNGLGETSHADERIVPROC glGetShaderiv;
PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation;
PFNGLUNIFORM1IPROC glUniform1i;
PFNGLUNIFORM1FPROC glUniform1f;
PFNGLACTIVETEXTUREPROC glActiveTexture;
PFNGLGETPROGRAMIVPROC glGetProgramiv;
PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog;
PFNGLVERTEXATTRIB1FPROC glVertexAttrib1f;
PFNGLVERTEXATTRIB2FPROC glVertexAttrib2f;
PFNGLVERTEXATTRIB3FPROC glVertexAttrib3f;
PFNGLGETATTRIBLOCATIONPROC glGetAttribLocation;
PFNGLBINDATTRIBLOCATIONPROC glBindAttribLocation;
PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer;
PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray;
PFNGLDISABLEVERTEXATTRIBARRAYPROC glDisableVertexAttribArray;
PFNGLGENBUFFERSPROC glGenBuffers;
PFNGLBINDBUFFERPROC glBindBuffer;
PFNGLBUFFERDATAPROC glBufferData;
PFNGLBUFFERSUBDATAPROC glBufferSubData;*/

DECLARE_GLEXTENSIONS

GLuint hprg,hvs,hfs;
GLint pos,texcoord,color;
GLint sampler2d_0;
GLint rotation;

namespace sprite{

struct MYSHEET{

	GLfloat pos[2];
	GLfloat texcoord[2];
	GLfloat color[4];

};

MYSHEET sheet[3]={{0.0,0.0,0.0,0.0,1.0,0.0,0.0,1.0},{1.0,0.0,0.0,0.0,0.0,1.0,0.0,1.0},{1.0,1.0,0.0,0.0,0.0,0.0,1.0,1.0}};

void* spriteLoadBitmap(TCHAR* filename){

	return (void*)auxDIBImageLoad(filename);

}

void InitEngine(){

	/*glCreateProgram=(PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram");
	glDeleteProgram=(PFNGLDELETEPROGRAMPROC)wglGetProcAddress("glDeleteProgram");
	glCreateShader=(PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader");
	glDeleteShader=(PFNGLDELETESHADERPROC)wglGetProcAddress("glDeleteShader");
	glShaderSource=(PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource");
	glCompileShader=(PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader");
	glAttachShader=(PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader");
	glDetachShader=(PFNGLDETACHSHADERPROC)wglGetProcAddress("glDetachShader");
	glLinkProgram=(PFNGLLINKPROGRAMPROC)wglGetProcAddress("glLinkProgram");
	glUseProgram=(PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram");
	glGetShaderInfoLog=(PFNGLGETSHADERINFOLOGPROC)wglGetProcAddress("glGetShaderInfoLog");
	glGetShaderiv=(PFNGLGETSHADERIVPROC)wglGetProcAddress("glGetShaderiv");
	glGetUniformLocation=(PFNGLGETUNIFORMLOCATIONPROC)wglGetProcAddress("glGetUniformLocation");
	glUniform1i=(PFNGLUNIFORM1IPROC)wglGetProcAddress("glUniform1i");
	glActiveTexture=(PFNGLACTIVETEXTUREPROC)wglGetProcAddress("glActiveTexture");
	glUniform1f=(PFNGLUNIFORM1FPROC)wglGetProcAddress("glUniform1f");
	glGetProgramiv=(PFNGLGETPROGRAMIVPROC)wglGetProcAddress("glGetProgramiv");
	glGetProgramInfoLog=(PFNGLGETPROGRAMINFOLOGPROC)wglGetProcAddress("glGetProgramInfoLog");
	glGetAttribLocation=(PFNGLGETATTRIBLOCATIONPROC)wglGetProcAddress("glGetAttribLocation");
	glVertexAttrib1f=(PFNGLVERTEXATTRIB1FPROC)wglGetProcAddress("glVertexAttrib1f");
	glVertexAttrib2f=(PFNGLVERTEXATTRIB2FPROC)wglGetProcAddress("glVertexAttrib2f");
	glVertexAttrib3f=(PFNGLVERTEXATTRIB3FPROC)wglGetProcAddress("glVertexAttrib3f");
	glBindAttribLocation=(PFNGLBINDATTRIBLOCATIONPROC)wglGetProcAddress("glBindAttribLocation");
	glVertexAttribPointer=(PFNGLVERTEXATTRIBPOINTERPROC)wglGetProcAddress("glVertexAttribPointer");
	glEnableVertexAttribArray=(PFNGLENABLEVERTEXATTRIBARRAYPROC)wglGetProcAddress("glEnableVertexAttribArray");
	glDisableVertexAttribArray=(PFNGLDISABLEVERTEXATTRIBARRAYPROC)wglGetProcAddress("glDisableVertexAttribArray");
	glGenBuffers=(PFNGLGENBUFFERSPROC)wglGetProcAddress("glGenBuffers");
	glBindBuffer=(PFNGLBINDBUFFERPROC)wglGetProcAddress("glBindBuffer");
	glBufferData=(PFNGLBUFFERDATAPROC)wglGetProcAddress("glBufferData");
	glBufferSubData=(PFNGLBUFFERSUBDATAPROC)wglGetProcAddress("glBufferSubData");*/

	GET_GLEXTENSIONS

	glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
	
	hprg=glCreateProgram();

	hvs=glCreateShader(GL_VERTEX_SHADER);
	hfs=glCreateShader(GL_FRAGMENT_SHADER);

	char* tvs;
	char* tfs;

	readfile(tvs,"vs.txt");
	readfile(tfs,"fs.txt");

	glShaderSource(hvs,1,(const GLchar**)&tvs,NULL);
	glShaderSource(hfs,1,(const GLchar**)&tfs,NULL);

	glCompileShader(hvs);
	printf("%s\n",tvs);
 	querysuccess(hvs);
		
	glCompileShader(hfs);
	printf("%s\n",tfs);
	querysuccess(hfs);
	
	int success;

	glAttachShader(hprg,hvs);
	glAttachShader(hprg,hfs);

	glBindAttribLocation(hprg,0,"pos");
	glLinkProgram(hprg);
	glGetProgramiv(hprg,GL_LINK_STATUS,&success);

	glUseProgram(hprg);	
	
	glGetProgramiv(hprg,GL_VALIDATE_STATUS,&success);
	
	int num;
	glGetProgramiv(hprg,GL_ACTIVE_UNIFORMS,&num);
	glGetProgramiv(hprg, GL_ACTIVE_ATTRIBUTES,&num);

	pos=glGetAttribLocation(hprg,"pos");
	texcoord=glGetAttribLocation(hprg,"texcoord");
	color=glGetAttribLocation(hprg,"color");
	
	int current;
	glGetIntegerv(GL_CURRENT_PROGRAM,&current);

	float arr[4]={1.0,0.0,0.0,1.0};

	sampler2d_0=glGetUniformLocation(hprg,"sampler2d_0");
	glUniform1i(sampler2d_0,0);
	rotation=glGetUniformLocation(hprg,"rotation");
	glUniformMatrix2fv(rotation,1,GL_FALSE,arr);

	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_BLEND);

	glEnableVertexAttribArray(pos);
	glEnableVertexAttribArray(texcoord);
	glEnableVertexAttribArray(color);

}

void spriteRefresh(GLint x, GLint y, GLsizei width, GLsizei height){

	glViewport(x,y,width,height);

}

void spriteDrawReady(HWND hWnd){

	glClearColor(0.0,0.0,0.0,1.0);
	glClear(GL_COLOR_BUFFER_BIT);

}

void spriteShow(HWND hWnd){

	HDC hdc=GetDC(hWnd);
	
	SwapBuffers(hdc);

	ValidateRect(hWnd,0);

}

}